﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Data;
using System.IO;
using System.Linq;

using Cinch;


namespace Anata.Client
{
    /// <summary>
    /// </summary>
    public class TransaksiThumbnailViewModel : ViewModelBase
    {
        #region Data

        //private ViewMode currentViewMode = ViewMode.AddMode;

        #region Data.Primitives
        private Int32 idTransaksi;
        private Int32? idKonsumen;
        private String namaKonsumen;
        private String alamatKonsumen;
        private String teleponKonsumen;
        private Int32? idCabang;
        private DateTime? waktuBuat;
        private DateTime? waktuSelesai;
        private Int32 koreksiHarga;
        private Boolean isBatal = false;

        private bool isBusy = false;
        #endregion

        //private IEnumerable<DataWrapperBase> cachedListOfDataWrappers;

        #region Data.Rules
        #endregion

        #region Data.VMs
        private DispatcherNotifiedObservableCollection<TransaksiLayananThumbnailViewModel> transaksiLayanans;
        private ICollectionView transaksiLayanansCV;
        private DispatcherNotifiedObservableCollection<TransaksiProdukThumbnailViewModel> transaksiProduks;
        private ICollectionView transaksiProduksCV;
        private DispatcherNotifiedObservableCollection<TransaksiPembayaranThumbnailViewModel> transaksiPembayarans;
        private ICollectionView transaksiPembayaransCV;
        #endregion

        #region Data.Services
        #endregion

        #endregion

        #region Constructor
        public TransaksiThumbnailViewModel()
        {
            #region Constructor.SetupServices
            #endregion

            #region Constructor.SetupCommands
            TransaksiPayCommand = new SimpleCommand<Object, Object>(CanExecuteTransaksiPayCommand, ExecuteTransaksiPayCommand);
            TransaksiCancelCommand = new SimpleCommand<Object, Object>(CanExecuteTransaksiCancelCommand, ExecuteTransaksiCancelCommand);
            #endregion

            #region Constructor.SetupDataWrappers
            #endregion

            //fetch list of all DataWrappers, so they can be used again later without the
            //need for reflection
            //cachedListOfDataWrappers =
            //    DataWrapperHelper.GetWrapperProperties<TransaksiThumbnailViewModel>(this);

            #region Constructor.SetupValidationRules
            #endregion

            #region Constructor.SetupVMs
            TransaksiLayanans = new DispatcherNotifiedObservableCollection<TransaksiLayananThumbnailViewModel>();
            TransaksiLayanansCV = CollectionViewSource.GetDefaultView(transaksiLayanans);
            TransaksiProduks = new DispatcherNotifiedObservableCollection<TransaksiProdukThumbnailViewModel>();
            TransaksiProduksCV = CollectionViewSource.GetDefaultView(transaksiProduks);
            TransaksiPembayarans = new DispatcherNotifiedObservableCollection<TransaksiPembayaranThumbnailViewModel>();
            TransaksiPembayaransCV = CollectionViewSource.GetDefaultView(transaksiPembayarans);
            #endregion

        }
        #endregion

        #region Static
        #endregion

        #region Public Properties
        #region Public.Commands
        public SimpleCommand<Object, Object> TransaksiPayCommand { get; private set; }
        public SimpleCommand<Object, Object> TransaksiCancelCommand { get; private set; }
        #endregion

        #region Public.ViewMode
        #endregion

        #region Public.Data
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idTransaksiChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.IdTransaksi);
        public Int32 IdTransaksi
        {
            get { return idTransaksi; }
            set
            {
                idTransaksi = value;
                NotifyPropertyChanged(idTransaksiChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idKonsumenChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.IdKonsumen);
        public Int32? IdKonsumen
        {
            get { return idKonsumen; }
            set
            {
                idKonsumen = value;
                NotifyPropertyChanged(idKonsumenChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs namaKonsumenChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.NamaKonsumen);
        public String NamaKonsumen
        {
            get { return namaKonsumen; }
            set
            {
                namaKonsumen = value;
                NotifyPropertyChanged(namaKonsumenChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs alamatKonsumenChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.AlamatKonsumen);
        public String AlamatKonsumen
        {
            get { return alamatKonsumen; }
            set
            {
                alamatKonsumen = value;
                NotifyPropertyChanged(alamatKonsumenChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs teleponKonsumenChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TeleponKonsumen);
        public String TeleponKonsumen
        {
            get { return teleponKonsumen; }
            set
            {
                teleponKonsumen = value;
                NotifyPropertyChanged(teleponKonsumenChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idCabangChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.IdCabang);
        public Int32? IdCabang
        {
            get { return idCabang; }
            set
            {
                idCabang = value;
                NotifyPropertyChanged(idCabangChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs waktuBuatChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.WaktuBuat);
        public DateTime? WaktuBuat
        {
            get { return waktuBuat; }
            set
            {
                waktuBuat = value;
                NotifyPropertyChanged(waktuBuatChangeArgs);
                NotifyPropertyChanged(statusTransaksiChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs waktuSelesaiChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.WaktuSelesai);
        public DateTime? WaktuSelesai
        {
            get { return waktuSelesai; }
            set
            {
                waktuSelesai = value;
                NotifyPropertyChanged(waktuSelesaiChangeArgs);
                NotifyPropertyChanged(statusTransaksiChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs koreksiHargaChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.KoreksiHarga);
        public Int32 KoreksiHarga
        {
            get { return koreksiHarga; }
            set
            {
                koreksiHarga = value;
                NotifyPropertyChanged(koreksiHargaChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs isBatalChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.IsBatal);
        public Boolean IsBatal
        {
            get { return isBatal; }
            set
            {
                isBatal = value;
                NotifyPropertyChanged(isBatalChangeArgs);
                NotifyPropertyChanged(statusTransaksiChangeArgs);
            }
        }



        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs isBusyChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.IsBusy);
        public Boolean IsBusy
        {
            get { return isBusy; }
            set
            {
                isBusy = value;
                NotifyPropertyChanged(isBusyChangeArgs);
            }
        }
        #endregion

        #region Public.Cached Collection
        #endregion

        #region Public.DataVM
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs transaksiLayanansChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TransaksiLayanans);
        public DispatcherNotifiedObservableCollection<TransaksiLayananThumbnailViewModel> TransaksiLayanans
        {
            get { return transaksiLayanans; }
            set
            {
                transaksiLayanans = value;
                NotifyPropertyChanged(transaksiLayanansChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs transaksiLayanansCVChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TransaksiLayanansCV);
        public ICollectionView TransaksiLayanansCV
        {
            get { return transaksiLayanansCV; }
            set
            {
                transaksiLayanansCV = value;
                NotifyPropertyChanged(transaksiLayanansCVChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs transaksiProduksChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TransaksiProduks);
        public DispatcherNotifiedObservableCollection<TransaksiProdukThumbnailViewModel> TransaksiProduks
        {
            get { return transaksiProduks; }
            set
            {
                transaksiProduks = value;
                NotifyPropertyChanged(transaksiProduksChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs transaksiProduksCVChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TransaksiProduksCV);
        public ICollectionView TransaksiProduksCV
        {
            get { return transaksiProduksCV; }
            set
            {
                transaksiProduksCV = value;
                NotifyPropertyChanged(transaksiProduksCVChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs transaksiPembayaransChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TransaksiPembayarans);
        public DispatcherNotifiedObservableCollection<TransaksiPembayaranThumbnailViewModel> TransaksiPembayarans
        {
            get { return transaksiPembayarans; }
            set
            {
                transaksiPembayarans = value;
                NotifyPropertyChanged(transaksiPembayaransChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs transaksiPembayaransCVChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TransaksiPembayaransCV);
        public ICollectionView TransaksiPembayaransCV
        {
            get { return transaksiPembayaransCV; }
            set
            {
                transaksiPembayaransCV = value;
                NotifyPropertyChanged(transaksiPembayaransCVChangeArgs);
            }
        }
        #endregion

        #region Public.SummaryData
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs subTotalHargaChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.SubTotalHarga);
        public Int32 SubTotalHarga
        {
            get
            {
                Int32 sum = 0;
                foreach (TransaksiLayananThumbnailViewModel tl in transaksiLayanans)
                {
                    sum += tl.HargaLayanan;
                }
                foreach (TransaksiProdukThumbnailViewModel  tp in transaksiProduks)
                {
                    sum += tp.SubTotalHargaProduk;
                }
                return sum;
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs totalPotonganChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TotalPotongan);
        public Int32 TotalPotongan
        {
            get
            {
                Int32 sum = 0;
                foreach (TransaksiLayananThumbnailViewModel tl in transaksiLayanans)
                {
                    sum += tl.PotonganPromo;
                }
                foreach (TransaksiProdukThumbnailViewModel tp in transaksiProduks)
                {
                    sum += tp.PotonganHarga;
                }
                return sum;
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs totalPembayaranChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TotalPembayaran);
        public Int32 TotalPembayaran
        {
            get
            {
                Int32 sum = 0;
                foreach (TransaksiPembayaranThumbnailViewModel tb in transaksiPembayarans)
                {
                    sum += tb.NominalPembayaran;
                }
                return sum;
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs totalHargaChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.TotalHarga);
        public Int32 TotalHarga
        {
            get
            {
                return SubTotalHarga - TotalPotongan;
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs kembalianChangeArgs =
            ObservableHelper.CreateArgs<TransaksiThumbnailViewModel>(x => x.Kembalian);
        public Int32 Kembalian
        {
            get
            {
                return TotalPembayaran - TotalHarga;
            }
        }

        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs statusTransaksiChangeArgs =
            ObservableHelper.CreateArgs<TransaksiViewModel>(x => x.StatusTransaksi);
        public int StatusTransaksi
        {
            get
            {
                int retval = 0;
                if (isBatal)
                {
                    retval = 99;
                }
                else if (WaktuSelesai == null)
                {
                    retval = 0;
                }
                else
                {
                    retval = 1;
                }
                return retval;
            }
        }
        #endregion

        #endregion

        #region Command Handlers
        /// <summary>
        /// </summary>
        private bool CanExecuteTransaksiPayCommand(Object args)
        {
            return !isBusy;
        }
        /// <summary>
        /// </summary>
        private void ExecuteTransaksiPayCommand(Object args)
        {
            Mediator.Instance.NotifyColleagues<TransaksiThumbnailViewModel>("TryPayTransaksiThumbnail", this);
        }

        /// <summary>
        /// </summary>
        private bool CanExecuteTransaksiCancelCommand(Object args)
        {
            return !isBusy;
        }
        /// <summary>
        /// </summary>
        private void ExecuteTransaksiCancelCommand(Object args)
        {
            Mediator.Instance.NotifyColleagues<TransaksiThumbnailViewModel>("TryCancelTransaksiThumbnail", this);
        }
        #endregion

        #region Mediator Message Sinks
        [MediatorMessageSink("SummaryChangeMessage")]
        private void SummaryChangeMessage(Boolean dummy)
        {
            NotifyPropertyChanged(subTotalHargaChangeArgs);
            NotifyPropertyChanged(totalPotonganChangeArgs);
            NotifyPropertyChanged(totalHargaChangeArgs);
            NotifyPropertyChanged(totalPembayaranChangeArgs);
            NotifyPropertyChanged(kembalianChangeArgs);
        }
        #endregion

        #region Private Methods
        #endregion

        #region Overrides
        public override string ToString()
        {
            return String.Format("TransaksiThumbnailViewModel {0}", this.idTransaksi);
        }
        #endregion

        #region EditableValidatingViewModelBase overrides
        #endregion
    }
}
